package com.company.app.test;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class HelloWorldSwingApp {
  private JFrame frame;

  public void createFrame() {
    // 初始化 JFrame
    frame = new JFrame("HelloWorldSwingApp JFrame");
    frame.setSize(300, 200);
    // 设置为 DO_NOTHING_ON_CLOSE 以自定义关闭行为
    frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
    frame.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        // 当窗口关闭时，执行回调函数
        printHelloWorld();
        frame.dispose(); // 关闭显示窗口
      }
    });
    frame.setVisible(true);
  }

  public void printHelloWorld() {
    System.out.println("Hello, world " + this);
  }

  public void test() {
    SwingUtilities.invokeLater(() -> {
      createFrame();
    });
  }
}